<template>
	<view class="layout">
		<view class="navbar">
			<view class="statusBar" :style="{height:getStatusBarHeight()+'px'}"></view>
			<view class="titleBar" :style="{height:getTitleBarHeight() + 'px'}">
				<view class="title">{{title}}</view>
				<view class="search">
					<uni-icons type="scan" size="30" @tap="goscan"></uni-icons>
				</view>
			</view>

		</view>
		<view class="fill" :style="{height:getNavBarHeight()+'px'}">

		</view>
	</view>
</template>

<script setup>
	import {
		reactive,
		ref,
		onMounted
	} from 'vue';
	import {
		getStatusBarHeight,
		getTitleBarHeight,
		getNavBarHeight
	} from "@/utils/system.js";
	import {
		apiInsertAttendence
	} from '../../api/apis';
	// import { scanCode } from '@dcloudio/uni-app';  //扫描二维码的api

	// 父子组件传递数据
	defineProps({
		title: {
			type: String,
			default: "首页"
		}
	})

	const insertAttandenceForm = reactive({
		studentId: null,
		lessonDate: null,
		timestamp: null,
		courseId: null
	})

	onMounted(() => {
		// 获取本地缓存
		uni.getStorage({
			key: 'studentInfo',
			success: function(r) {
				const studentVO = r.data;
				insertAttandenceForm.studentId = studentVO.id;
			},
		});
	})


	const handleInsertAttendence = async function() {
		const res = await apiInsertAttendence(insertAttandenceForm);
		if (res.code === 0) {
			uni.showToast({
				title: "签到成功！",
				icon: 'success',
				duration: 2000
			})
		} else {
			uni.showToast({
				title: "签到失败！" + res.msg,
				icon: 'error',
				duration: 2000
			})
		}
	}

	// 扫描功能
	const goscan = () => {
		uni.scanCode({
			onlyFromCamera: true,
			autoZoom: true,
			success: function(res) {

				// 手动解析二维码返回的字符串
				const queryString = res.result;
				const params = {};
				queryString.split("&").forEach(pair => {
					const [key, value] = pair.split("=");
					params[key] = decodeURIComponent(value); // 解码以防含有特殊字符
				});

				// 提取 lessonDate 和 timestamp
				const lessonDate = params.lessonDate;
				const timestamp = params.timestamp;
				const courseId = params.courseId;

				// 将数据保存到表单
				insertAttandenceForm.lessonDate = lessonDate;
				insertAttandenceForm.timestamp = timestamp;
				insertAttandenceForm.courseId = courseId;

				handleInsertAttendence()

			},
			fail: function(err) {
				console.error("扫码失败：", err);
			}
		});
		console.log("ok");
	};
</script>

<style lang="scss" scoped>
	.layout {
		.navbar {
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			z-index: 10;

			.statusBar {
				// border: solid 1px red;
			}

			background: linear-gradient(to bottom, rgba(0, 0, 0, 0), #fff 400rpx),
			linear-gradient(to right, #beecd8, #f4e2db);

			.titleBar {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 0 30rpx;

				.title {
					font-size: 22px;
					font-weight: 700;
					color: $text-font-color-1;
				}

				.search {
					display: flex;
					align-items: center; // 确保图标垂直居中对齐

					.search uni-icons {
						size: 30;
						margin-right: 10rpx;
					}
				}
			}
		}

	}
</style>